<?php

namespace App\Http\Controllers\Api;

use App\Models\Archive\Archive;
use App\Models\Live\Anchor;
use App\Models\Tag\Tag;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SiteMap  extends Controller
{
    public function index()
    {
        $user = session('user');

        if (empty($user) || !$user->master) abort(403);
        ini_set('memory_limit', '-1');
        $archives = Archive::ofPattern('review')->where('is_del', '0')->get(['id', 'archive_type_id', 'title']);
        $tags = Tag::where('status', '2')->take(10000)->get(['name', 'current_url', 'abbr', 'pinyin']);
        $anchors = Anchor::orderBy('online', 'desc')->whereNotIn('live_id', [1,5,9])->take(50000 - $archives->count() - $tags->count())->get(['id', 'name']);
        $url = route('index');
        $time = date('Y-m-d');
        $xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<urlset
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
       http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url><loc>{$url}</loc><priority>1.00</priority><lastmod>{$time}</lastmod><changefreq>weekly</changefreq> </url>
    {$archives->map(function ($id) use($url, $time) {
            return "<url><loc>{$url}/{$this->getType($id->archive_type_id)}/{$id->id}.html</loc> <lastmod>{$time}</lastmod><changefreq>Weekly</changefreq><priority>0.5</priority></url>";
        })
            ->implode("\n")
        }
    {$tags->map(function ($id) use($url, $time) {
            return "<url><loc>{$url}{$id->url}</loc><lastmod>{$time}</lastmod><changefreq>Weekly</changefreq><priority>0.5</priority></url>";
        })
            ->implode("\n")
        }
    {$anchors->map(function ($anchor) use($url, $time) {

            return "<url><loc>{$url}/anchors/{$anchor->id}/preview</loc><lastmod>{$time}</lastmod><changefreq>Weekly</changefreq><priority>0.5</priority></url>";

        })->implode("\n")

        }
</urlset>
XML;

        $html_head = <<<HTML_HEAD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<title>网站地图-奇异果-看点有意思的</title>
<meta http-equiv="Content-type" content="text/html;" charset="utf-8" />
<style type="text/css">
<!-- 
 .STYLE1 {
 font-size: 12px;
 color: #333333;
 }
 -->
 </style>
</head>
 <body>
 <table align="center">
 <tr  align="center">
 <td align="center">
 <table width="766" border="0" >
<tr align="left">
 <td class="STYLE1" ><span >网站地图：({$time})</span></td>
 </tr>

HTML_HEAD;

        $html_foot = <<<HTML_FOOT
 </table> </td> </tr> </table><br />
HTML_FOOT;

        $html = $html_head.$archives->map(function ($item, $index) use($url) {
                return <<<ITEM
            <tr align="left"> 
                <td width="760" class="STYLE1"><a href="{$url}/{$this->getType($item->archive_type_id)}/{$item->id}.html">{$item->title}</a></td>
            </tr>
ITEM;

            })
                ->implode("\n")
            .$tags->map(function ($item, $index) use($url) {
                return <<<ITEM
            <tr align="left"> 
                <td width="760" class="STYLE1"><a href="{$url}{$item->url}">{$item->name}</a></td>
            </tr>
ITEM;

            })->implode("\n")
            .$anchors->map(function ($item, $index) use($url) {
                return <<<ITEM
            <tr align="left"> 
                <td width="760" class="STYLE1"><a href="{$url}/anchors/{$item->id}/preview">{$item->name}官方直播间地址_{$item->name}微博地址_{$item->name}视频_{$item->name}最新动态</a></td>
            </tr>
ITEM;

            })->implode("\n")
            .$html_foot;

        $txt = $archives->map(function ($item, $index) use($url) {
                return "{$url}/{$this->getType($item->archive_type_id)}/{$item->id}.html";

            })
                ->implode("\n")."\n"
            .$tags->map(function ($item, $index) use($url) {
                return "{$url}{$item->url}";
            })->implode("\n")."\n"
            .$anchors->map(function ($item, $index) use($url) {
                return "{$url}/anchors/{$item->id}/preview";

            })->implode("\n");
        file_put_contents(public_path('sitemap.xml'), $xml);
        file_put_contents(public_path('sitemap.htm'), $html);
        file_put_contents(public_path('sitemap.txt'), $txt);
        \DB::table('global')->update(['sitemap_update_time' => $time]);
        return response()->json(['msg' => '生成成功。', 'date' => $time]);
    }

    private function getType($type_id)
    {
        return $type_id == 1 || $type_id == 4 ? 'article' : ($type_id == 2 ? 'gallery' : 'video');
    }
}